package com.cos.controller;

import com.cos.commons.R;
import com.cos.fegin.api.GoodsTypeService;
import com.cos.vo.GoodsTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;
    @RequestMapping("/{page}.html")
    public String page(@PathVariable("page") String page , Integer id,ModelMap modelMap) {
       modelMap.put("id",id);
        return "goodsType/"+page;
    }
    @GetMapping ("/getGoodsTypeTree")
    @ResponseBody
    public R getGoodsTypeTree(){
        List<GoodsTypes> list = goodsTypeService.getList();
        //创建一个list来储存所有的节点
        List<Map<String,Object>> list1=new ArrayList<>();
        for (GoodsTypes goodsTypes : list) {
            //用来储存所有节点的信息
            Map<String,Object> map=new HashMap<>();
            map.put("id",goodsTypes.getId());
            map.put("name",goodsTypes.getGoodsTypeName());
            map.put("pid",goodsTypes.getGoodsTypePid());
            list1.add(map);
        }
        return R.success(list1);
    }
    @RequestMapping("/getList")
    public String getList(ModelMap modelMap){
        List<GoodsTypes> list = goodsTypeService.getList();
        modelMap.put("goodsTypeList",list);
        return "/goodsType/goodsTypeList";
    }

    @RequestMapping("/addGoodsType")
    @ResponseBody
    public R addGoodsType(GoodsTypes goodsTypes){
        return R.write(goodsTypeService.addGoodsType(goodsTypes));
    }

    @PostMapping("/updateGoodsType")
    @ResponseBody
    public R updateGoodsType( GoodsTypes goodsTypes){
        return  R.write(goodsTypeService.updateGoodsType(goodsTypes));
    }

    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    public R deleteById(@PathVariable Integer id ){
        return R.write(goodsTypeService.deleteById(id));
    }
}
